package com.itheima.myCollection;

import java.util.ArrayList;
import java.util.Iterator;

public class Demo3 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("张三",21);
        Student stu2 = new Student("李四",21);
        Student stu3 = new Student("王五",21);
        //添加元素
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        //遍历集合
        Iterator<Student> it = list.iterator();
        while(it.hasNext()){
            Student s = it.next();
            System.out.println(s);
        }
        System.out.println("--------------");
        //第二种
        for (Student student : list) {
            System.out.println(student);
        }
        System.out.println("--------------");
        //第三种
        for (int i = 0; i < list.size(); i++) {
            System.out.println(stu1.getName()+"..."+stu1.getAge());
            System.out.println(stu2.getName()+"..."+stu2.getAge());
            System.out.println(stu3.getName()+"..."+stu3.getAge());
        }
    }
}
class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}